package com.demo1112.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/demo1112/demo03.do")
public class Demo03Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //直接覆盖原有的Cookie
        //1.创建一个同名Cookie，Value不同
        Cookie cookie = new Cookie("key1", "value1value1");
        //2.通知浏览器接收cookie
        resp.addCookie(cookie);


        //先设置生命周期
        //取值3种
        //1.负数，通常取-1：表示Session会话级别的Cookie，也就是浏览器一旦关闭，Cookie就会被删除
        //2.0：马上删除Cookie
        //3.正数：单位为秒，在指定的秒数后过期

        //再通知浏览器保存cookie
        Cookie cookie1 = new Cookie("cookie01", "cookieValue01");
        cookie1.setMaxAge(-1);
        Cookie cookie2 = new Cookie("key5", "value5");
        cookie2.setMaxAge(0);
        Cookie cookie3 = new Cookie("cookie3", "cookieValue03");
        cookie3.setMaxAge(60 * 60 * 24);
        resp.addCookie(cookie1);
        resp.addCookie(cookie2);
        resp.addCookie(cookie3);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
